﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using JasLib.Interop.Win32;

namespace JasLib.Wpf
{
    /// <summary>
    /// Used by a viewmodel to inform or question the user without adhering specifically to any view.
    /// </summary>
    public interface IDialogInvoker
    {
        MessageBoxResult Prompt(
            ViewModelBase source,
            object objContext,
            string strPrompt,
            string strCaption,
            MessageBoxButton buttons,
            MessageBoxResult defaultResult,
            MessageBoxImage icon,
            MessageBoxOptions options);

        System.Windows.Forms.DialogResult ShowCommonDialog(
            ViewModelBase source,
            object objContext,
            System.Windows.Forms.CommonDialog dialog);

        bool? ShowCommonDialog(
            ViewModelBase source,
            object objContext,
            Microsoft.Win32.CommonDialog dialog);

        IDisposable NotifyWait(ViewModelBase source, object objContext);
        void NotifyWaitUntilIdle(ViewModelBase source, object objContext);
        IDisposable NotifyAsyncWait(ViewModelBase source, object objContext);
    }

    public class DialogInvokerBase : IDialogInvoker
    {
        public virtual MessageBoxResult Prompt(
            ViewModelBase source = null,
            object objContext = null,
            string strPrompt = null,
            string strCaption = null,
            MessageBoxButton buttons = MessageBoxButton.OK,
            MessageBoxResult defaultResult = MessageBoxResult.OK,
            MessageBoxImage icon = MessageBoxImage.None,
            MessageBoxOptions options = MessageBoxOptions.None)
        {
            return MessageBox.Show(strPrompt, strCaption, buttons, icon);
        }

        public virtual System.Windows.Forms.DialogResult ShowCommonDialog(
            ViewModelBase source,
            object objContext,
            System.Windows.Forms.CommonDialog dialog)
        {
            return dialog.ShowDialog();
        }

        public virtual bool? ShowCommonDialog(
            ViewModelBase source,
            object objContext,
            Microsoft.Win32.CommonDialog dialog)
        {
            return dialog.ShowDialog();
        }

        public virtual IDisposable NotifyWait(ViewModelBase source, object objContext)
        {
            throw new NotImplementedException();
        }

        public virtual void NotifyWaitUntilIdle(ViewModelBase source, object objContext)
        {
            throw new NotImplementedException();
        }

        public virtual IDisposable NotifyAsyncWait(ViewModelBase source, object objContext)
        {
            throw new NotImplementedException();
        }
    }

    public static class DialogInvokerExtensions
    {
        public static IDisposable SafeNotifyWait(this IDialogInvoker dialogInvoker, ViewModelBase source = null, object objContext = null)
        {
            IDisposable disposable = null;

            if (dialogInvoker != null)
            {
                disposable = dialogInvoker.NotifyWait(source, objContext);
            }

            return disposable ?? new DoNothingDisposable();
        }
    }
}
